package com.zl.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zl.payment.entity.OverdueBill;
import com.zl.payment.entity.OverdueRecord;
import com.zl.payment.mapper.OverdueBillMapper;
import com.zl.payment.mapper.OverdueRecordMapper;
import com.zl.payment.service.IOverdueBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.payment.service.IOverdueRecordService;
import com.zl.payment.service.IRepaymentRecordService;
import com.zl.payment.util.DateUtils;
import com.zl.payment.util.PaymentType;
import javafx.scene.control.OverrunStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 黄军鹏
 * @since 2020-07-17
 */
@Service
public class OverdueBillServiceImpl extends ServiceImpl<OverdueBillMapper, OverdueBill> implements IOverdueBillService {
    @Autowired
    private OverdueBillMapper overdueBillMapper;
    @Autowired
    private OverdueRecordMapper overdueRecordMapper;
    @Autowired
    private IRepaymentRecordService repaymentRecordService;
    //获取所有逾期未还账单
    public List<OverdueBill> getOverbills(String creditCardId){
        List<OverdueBill> overdueBills = new ArrayList<>();
        QueryWrapper<OverdueRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CREDIT_CARD_ID",creditCardId);
        queryWrapper.eq("OVER_STATUS",0);
        List<OverdueRecord> records = overdueRecordMapper.selectList(queryWrapper);
        for (OverdueRecord record : records) {
            OverdueBill overdueBill = overdueBillMapper.selectOne(new QueryWrapper<OverdueBill>().eq("OVERDUE_BILL_ID", record.getBillId()));
            overdueBills.add(overdueBill);
        }
        return overdueBills;
    }
    public BigDecimal interest (OverdueBill overdueBill){
        BigDecimal simpleRemian = overdueBill.getSimpleRemian();
        BigDecimal installmentRemain = overdueBill.getInstallmentRemain();
        //逾期的利息计算（从出账到还款之间的天数*0.0005*金额）
        System.out.println(DateUtils.getIntervalDays(overdueBill.getOverdueDate()) + 22 + "");
        BigDecimal interest = (simpleRemian.add(installmentRemain)).multiply(new BigDecimal("0.0005")).multiply(new BigDecimal(DateUtils.getIntervalDays(overdueBill.getOverdueDate())+22+""));
        return interest;
    }
    
    public BigDecimal totalAmount(OverdueBill overdueBill){
        BigDecimal simpleRemian = overdueBill.getSimpleRemian();
        BigDecimal installmentRemain = overdueBill.getInstallmentRemain();
        BigDecimal lateFee = overdueBill.getLateFee();
        BigDecimal interest = interest(overdueBill);
        return simpleRemian.add(installmentRemain).add(lateFee).add(interest);
    }
    
    public boolean repayOverdueBill( OverdueBill overdueBill,String cardNo,BigDecimal amount){
        BigDecimal totalAmount = totalAmount(overdueBill);
        QueryWrapper<OverdueRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BILL_ID",overdueBill.getOverdueBillId());
        OverdueRecord overdueRecord = overdueRecordMapper.selectOne(queryWrapper);
        //核对还款金额
        if (totalAmount.compareTo(amount)==0){
            //还款操作，添加还款记录
            boolean repayment = repaymentRecordService.repayment(overdueBill.getCreditCardId(), overdueBill.getOverdueBillId(), PaymentType.逾期还款, amount, cardNo);
            if (repayment){
                //更改逾期账单状态和逾期记录
                overdueBill.setOverdueStatus(1);
                int i = overdueBillMapper.updateById(overdueBill);
                overdueRecord.setOverStatus(1);
                overdueRecord.setRepayDate(DateUtils.today());
                overdueRecord.setOverdueDays(DateUtils.getIntervalDays(overdueRecord.getOverdueDate()));
                System.out.println(DateUtils.getIntervalDays(overdueRecord.getOverdueDate()));
                int j = overdueRecordMapper.updateById(overdueRecord);
                if (i==1&&j==1) return true;
            }
        }
        return false;
    }
}

